package com.oauth.global.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasMouseOutHandlers;
import com.google.gwt.event.dom.client.HasMouseOverHandlers;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.components.client.ui.CommonResources;
import com.oauth.components.client.ui.HasWidgetInfo;

public class WidgetMenuItem extends Composite implements HasClickHandlers, HasMouseOutHandlers,
		HasMouseOverHandlers {

	private static ApplicationItemUiBinder uiBinder = GWT.create(ApplicationItemUiBinder.class);

	interface ApplicationItemUiBinder extends UiBinder<Widget, WidgetMenuItem> {
	}

	@UiField
	HorizontalPanel itemPanel;

	@UiField
	Image icon;

	@UiField
	Label name;

	public WidgetMenuItem(String name, Image icon) {
		initWidget(uiBinder.createAndBindUi(this));
		WidgetMenuMouseHandlers mouseHandler = new WidgetMenuMouseHandlers();
		addMouseOutHandler(mouseHandler);
		addMouseOverHandler(mouseHandler);
		this.icon.setUrl(icon.getUrl());
		this.name.setText(name);
	}

	public WidgetMenuItem(HasWidgetInfo widgetInfo) {
		initWidget(uiBinder.createAndBindUi(this));
		WidgetMenuMouseHandlers mouseHandler = new WidgetMenuMouseHandlers();
		addMouseOutHandler(mouseHandler);
		addMouseOverHandler(mouseHandler);
		// image.setUrl(pApplication.getIconImageUrl());
		name.setText(widgetInfo.getName());
		icon.setUrl(widgetInfo.getIcon().getUrl());
	}

	public void setName(String name) {
		this.name.setText(name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.google.gwt.event.dom.client.HasClickHandlers#addClickHandler(com.
	 * google.gwt.event.dom.client.ClickHandler)
	 */
	@Override
	public HandlerRegistration addClickHandler(ClickHandler handler) {
		return name.addClickHandler(handler);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.google.gwt.event.dom.client.HasMouseOverHandlers#addMouseOverHandler
	 * (com.google.gwt.event.dom.client.MouseOverHandler)
	 */
	@Override
	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
		return name.addMouseOverHandler(handler);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.google.gwt.event.dom.client.HasMouseOutHandlers#addMouseOutHandler
	 * (com.google.gwt.event.dom.client.MouseOutHandler)
	 */
	@Override
	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
		return name.addMouseOutHandler(handler);
	}

	private class WidgetMenuMouseHandlers implements MouseOutHandler, MouseOverHandler {

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * com.google.gwt.event.dom.client.MouseOverHandler#onMouseOver(com.
		 * google.gwt.event.dom.client.MouseOverEvent)
		 */
		@Override
		public void onMouseOver(MouseOverEvent event) {
			itemPanel.addStyleName(CommonResources.INSTANCE.css().highlightCursor());
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * com.google.gwt.event.dom.client.MouseOutHandler#onMouseOut(com.google
		 * .gwt.event.dom.client.MouseOutEvent)
		 */
		@Override
		public void onMouseOut(MouseOutEvent event) {
			itemPanel.removeStyleName(CommonResources.INSTANCE.css().highlightCursor());
		}

	}

}
